nition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) 
    {
        // 二叉树的前序非递归遍历
        vector<int> ans;
        if (root == nullptr)
        {
            return ans;
        }

        stack<TreeNode*> to_ans;
        to_ans.push(root);
        

        while(!to_ans.empty())
        {
            TreeNode* top = to_ans.top();
            to_ans.pop();
            ans.push_back(top->val);
            if (top->right)
            {
                to_ans.push(top->right);
            }
            if (top->left)
            {
                to_ans.push(top->left);
            }

            
        }

        return ans;
